/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package entities.tools;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import util.other.ENUMQuestionTypes;

//TODO: I think that this entity should extend the FormElementEntity. There should be other extended Entities like: Tittle, Text, Section, Page, Description,
/**
 *
 * @author Marek
 */
@Entity
public class Question implements Serializable {

    private static final long serialVersionUID = 1L;
    /**
     *
     */
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

        /**
     *
     */
    private int orderNumber = 0;

    /**
     *
     */
    private String question = "";
    /**
     *
     */
    private String description = "";
    /**
     *
     */
    private ENUMQuestionTypes type = ENUMQuestionTypes.TEXT;

    /**
     *
     */
    @OneToMany(cascade = (CascadeType.PERSIST), fetch = FetchType.EAGER, mappedBy = "question")
    private List<Answer> answers = new ArrayList<>();

    /**
     * Questions can have subQuestions. Here we have bidirectional relation one
     * to many to perform this.
     *
     * @author Marek
     */
    @OneToMany(cascade = (CascadeType.ALL), fetch = FetchType.EAGER, mappedBy = "parentQuestion", orphanRemoval = true)
    private List<Question> childQuestions = new ArrayList<>();

    /**
     *
     */
    @ManyToOne
    private Question parentQuestion;

    /**
     *
     */
    @ManyToOne
    private Form form;

    /**
     *
     * @return id
     */
    public Long getId() {
        return id;
    }

    /**
     *
     * @param id
     */
    public void setId(Long id) {
        this.id = id;
    }

    /**
     *
     * @return
     */
    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    /**
     *
     * @param object
     * @return
     */
    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Question)) {
            return false;
        }
        Question other = (Question) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    /**
     *
     * @return the string representation of the Question entity
     */
    @Override
    public String toString() {
        return "entities.tools.Question[ id=" + id + " ]";
    }

    /**
     * @return the questions
     */
    public List<Question> getChildQuestions() {
        return childQuestions;
    }

    /**
     * @param childQuestions the questions to set
     */
    public void setChildQuestions(List<Question> childQuestions) {
        this.childQuestions = childQuestions;
    }

    /**
     * @return the parentQuestion
     */
    public Question getParentQuestion() {
        return parentQuestion;
    }

    /**
     * @param parentQuestion the parentQuestion to set
     */
    public void setParentQuestion(Question parentQuestion) {
        this.parentQuestion = parentQuestion;
        this.setForm(parentQuestion.getForm());
    }

    /**
     * @return the question
     */
    public String getQuestion() {
        return question;
    }

    /**
     * @param question the question to set
     */
    public void setQuestion(String question) {
        this.question = question;
    }

    /**
     * @return the description
     */
    public String getDescription() {
        return description;
    }

    /**
     * @param description the description to set
     */
    public void setDescription(String description) {
        this.description = description;
    }

    /**
     * @return the type
     */
    public ENUMQuestionTypes getType() {
        return type;
    }

    /**
     * @param type the type to set
     */
    public void setType(ENUMQuestionTypes type) {
        this.type = type;
    }

    /**
     * @return the form
     */
    public Form getForm() {
        return form;
    }

    /**
     * @param form the form to set
     */
    public void setForm(Form form) {
        this.form = form;
    }

    /**
     * @return the answers
     */
    public List<Answer> getAnswers() {
        return answers;
    }

    /**
     * @param answers the answers to set
     */
    public void setAnswers(List<Answer> answers) {
        this.answers = answers;
    }

    /**
     * @return the orderNumber
     */
    public int getOrderNumber() {
        return orderNumber;
    }

    /**
     * @param orderNumber the orderNumber to set
     */
    public void setOrderNumber(int orderNumber) {
        this.orderNumber = orderNumber;
    }

}
